package com.gs.synedocket.presenter;

import com.google.gson.reflect.TypeToken;
import com.gs.synedocket.apiresult.ApiResultData;
import com.gs.synedocket.constant.AllConstants;
import com.gs.synedocket.db.dto.Base64PhotoDto;
import com.gs.synedocket.db.dto.ConfirmDocketDto;
import com.gs.synedocket.db.dto.DocketAccRepDto;
import com.gs.synedocket.db.dto.DocketAddChargeDto;
import com.gs.synedocket.model.DocketModel;
import com.gs.synedocket.model.UtilModel;
import com.gs.synedocket.model.listener.OnResultListener;
import com.gs.synedocket.view.IDocketAdditionalView;

import java.lang.reflect.Type;

public class DocketAdditionalPresenter extends BasePresenter<IDocketAdditionalView> {

    private UtilModel utilsModel = new UtilModel();
    private DocketModel docketModel = new DocketModel();

    public void upload(String path) {
        if (getView() != null) {
            Type typeofT = new TypeToken<ApiResultData<String>>() {
            }.getType();
            utilsModel.uploadOffline(path, new OnResultListener() {
                @Override
                public void onSuccess(Object aipResult) {
                    if (getView() != null) {
                        getView().onUploadFileSuccess((ApiResultData<String>) aipResult);
                    }
                }

                @Override
                public void onFails(Exception e) {
                    if (getView() != null) {
                        getView().onUploadFileFails(e);
                        getView().hideLoading();
                    }
                }
            });
        }
    }

    public void uploadBase64(String path,Boolean isEncrypted) {
        if (getView() != null) {
            Base64PhotoDto bean = new Base64PhotoDto();
            bean.setBase64(path);
            bean.setExeName(AllConstants.BASE64_NAME);
            bean.setEncrypted(isEncrypted);
            utilsModel.uploadBase64Offline(bean, new OnResultListener() {
                @Override
                public void onSuccess(Object aipResult) {
                    if (getView() != null) {
                        getView().onUploadFileSuccess((ApiResultData<String>) aipResult);
                    }
                }

                @Override
                public void onFails(Exception e) {
                    if (getView() != null) {
                        getView().onUploadFileFails(e);
                        getView().hideLoading();
                    }
                }
            });
        }
    }

    public void additionalCharge(DocketAddChargeDto bean) {
        if (getView() != null) {
            Type typeofT = new TypeToken<ApiResultData<String>>() {
            }.getType();
            docketModel.additionalCharge(bean, new OnResultListener() {
                @Override
                public void onSuccess(Object aipResult) {
                    if (getView() != null) {
                        getView().hideLoading();
                        getView().onUpdateSuccess((ApiResultData<String>) aipResult);
                    }
                }

                @Override
                public void onFails(Exception e) {
                    if (getView() != null) {
                        getView().hideLoading();
                        getView().onUpdateFails(e);
                    }
                }
            }, typeofT);
        }
    }

    public void comWithoutSign(ConfirmDocketDto bean) {
        if (getView() != null) {
            Type typeofT=new TypeToken<ApiResultData<String>>(){}.getType();
            docketModel.comWithoutSign(bean, new OnResultListener() {
                @Override
                public void onSuccess(Object aipResult) {
                    if (getView() != null) {
                        getView().hideLoading();
                        getView().onWithoutSignSuccess((ApiResultData<String>)aipResult);
                    }
                }

                @Override
                public void onFails(Exception e) {
                    if (getView() != null) {
                        getView().hideLoading();
                        getView().onWithoutSignFails(e);
                    }
                }
            },typeofT);
        }
    }

    public void addAccRep(DocketAccRepDto bean) {
        if (getView() != null) {
            getView().showLoading();
            Type typeofT=new TypeToken<ApiResultData<String>>(){}.getType();
            docketModel.addAccRep(bean, new OnResultListener() {
                @Override
                public void onSuccess(Object aipResult) {
                    if (getView() != null) {
                        getView().hideLoading();
                        getView().onAddInfoSuccess((ApiResultData<String>)aipResult);
                    }
                }

                @Override
                public void onFails(Exception e) {
                    if (getView() != null) {
                        getView().hideLoading();
                        getView().onAddInfoFails(e);
                    }
                }
            },typeofT);
        }
    }
}
